﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Configuration;
using System.Security.Principal;
using System.Web;
using System.Web.Caching;
using System.Web.Razor.Generator;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Tools;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;

namespace Web.Service
{
    public  class CacheService : BaseService
    {
        public  static Cache cacheService = HttpRuntime.Cache;
        private const String REPORTLIST = "REPORTLIST";

        private static CacheService instance;

        private CacheService()
        {

        }

        public static CacheService GetInstance()
        {
            if (instance == null)
            {
                instance = new CacheService();
            }
            return instance;
        }

        #region MyRegion

        /// <summary>
        /// 获取用户报文列表
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public static List<ReportModel> GetReport(string account)
        {
            if (cacheService.Get(account) == null)
            {
                User user = UserOp.GetInstance().QueryUser(account);
                if (user == null)
                    return new List<ReportModel>();
                var list = GetAllReport().Where(m => user.MMSI.Contains(m.MMSI)).ToList();

                PutReport(account,list);
            }
            return (List<ReportModel>)cacheService[account];
        }
        /// <summary>
        /// 添加用户报文缓存
        /// </summary>
        /// <param name="account"></param>
        /// <param name="list"></param>
        public static void PutReport(string account, List<ReportModel> list)
        {
            
            cacheService.Insert(account, list, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration,
                CacheItemPriority.High, UpdateReport);
        }
        /// <summary>
        /// 更新用户报文列表
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="call"></param>
        public static void UpdateReport(string key, object value, CacheItemRemovedReason call)
        {
            if(call== CacheItemRemovedReason.Expired)
            { 
                var list = GetAllReport().Where(m => UserOp.GetInstance().QueryUser(key).MMSI.Contains(m.MMSI)).ToList();
                PutReport(key, list);
            }else if (call == CacheItemRemovedReason.Removed)
            {
                cacheService.Remove(key);
            }
        }

        #endregion

        #region 缓存所有报文列表
        /// <summary>
        /// 获取所有报文列表
        /// </summary>
        /// <returns></returns>
        public static List<ReportModel> GetAllReport()
        {
            if (cacheService[REPORTLIST] == null)
            {
                PutAllReport(CacheData().Select(m => ReportModel.ConvertMondel(m)).ToList());
            }
            return (List<ReportModel>)cacheService[REPORTLIST];
        }

        /// <summary>
        /// 添加所有报文列表的缓存
        /// </summary>
        /// <param name="list"></param>
        public static void PutAllReport(List<ReportModel> list)
        {

            cacheService.Insert(REPORTLIST, list, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration,
                CacheItemPriority.High, UpdateAllReport);
        }

        /// <summary>
        /// 更新所有报文列表的缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="call"></param>
        public static void UpdateAllReport(string key, object value, CacheItemRemovedReason call)
        {
            if (call == CacheItemRemovedReason.Expired)
            {
                PutAllReport(
                   CacheData().Select(m => ReportModel.ConvertMondel(m)).ToList());
            }
        }

        private static List<Report> CacheData()
        {
            var list =
                    ReportOp.GetInstance().QueryAllReportList();
            var temp = list.Where(m => m.ReportTime == 0 );
            if (temp.Count() > 0)
            {
                ReportService.GetInstance().ModifReports(temp.ToList());
                list = ReportOp.GetInstance().QueryAllReportList().ToList();
            }
            return list;
        }
        


        #endregion






    }
}